package uk.ac.tess.timemanagement.todolist;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.style.StrikethroughSpan;
import android.util.Log;
import android.view.LayoutInflater;
import uk.ac.tess.timemanagement.R;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class ToDoArrayAdapter extends ArrayAdapter<ToDoItem> {

	private List<ToDoItem> items;
	private Context context;
	private StrikethroughSpan strikethrough = new StrikethroughSpan();
	private ToDoDatabaseHandler db;

	public ToDoArrayAdapter(Context context, int textViewResourceId,
			List<ToDoItem> items) {
		super(context, textViewResourceId, items);
		this.items = items;
		this.context = context;
		this.db = new ToDoDatabaseHandler(this.context);
	}

	public ToDoItem getToDoItem(int position) {
		if (position >= 0 && position < this.items.size()) {
			return this.items.get(position);
		}
		return null;
	}  

	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		if (row == null) {
			// ROW INFLATION
			LayoutInflater inflater = (LayoutInflater) this.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.todo_row, parent, false);
		}
		row.setClickable(true);
		
		row.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Log.d("Log", "row clicked change to edit");
			}
		});
		
		row.setOnLongClickListener(new View.OnLongClickListener() {

			public boolean onLongClick(View v) {
				Log.d("Log", "Row long clicked change menu options");
				return true;
			}
		});

		final ToDoItem item = this.getToDoItem(position);

		final CheckBox isActive = (CheckBox) row
				.findViewById(R.id.todo_row_active);
		final TextView description = (TextView) row
				.findViewById(R.id.todo_row_description);
		final TextView date = (TextView) row.findViewById(R.id.todo_row_date);
		final TextView time = (TextView) row.findViewById(R.id.todo_row_time);

		isActive.setChecked(item.isItemActive());
		description.setText(item.getItemDescription());
		date.setText(item.getItemDate());
		time.setText(item.getItemTime());

		setText(description, date, time, item.isItemActive());

		isActive.setClickable(true);
		isActive.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				boolean change = isActive.isChecked();
				setText(description, date, time, change);
				item.setItemActive(change);
				db.updateToDoItem(item);
			}
		});

		return row;
	}

	public void setText(TextView description, TextView date, TextView time,
			boolean isChecked) {
		SpannableString spannableDescription = new SpannableString(
				description.getText());
		SpannableString spannableDate = new SpannableString(date.getText());
		SpannableString spannableTime = new SpannableString(time.getText());
		int color;
		if (isChecked) {
			spannableDescription.setSpan(strikethrough, 0, description
					.getText().length(), 0);
			spannableDate.setSpan(strikethrough, 0, date.getText().length(), 0);
			spannableTime.setSpan(strikethrough, 0, time.getText().length(), 0);
			color = Color.parseColor("#77888888");
		} else {
			spannableDescription.removeSpan(strikethrough);
			spannableDate.removeSpan(strikethrough);
			spannableTime.removeSpan(strikethrough);
			color = Color.parseColor("#ff111111");
		}

		description
				.setText(spannableDescription, TextView.BufferType.SPANNABLE);
		description.setTextColor(color);
		date.setText(spannableDate, TextView.BufferType.SPANNABLE);
		date.setTextColor(color);
		time.setText(spannableTime, TextView.BufferType.SPANNABLE);
		time.setTextColor(color);
	}
}
